package laugh.util.db;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import laugh.util.Constants;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 
*    
* 项目名称：LaughFactory   
* 类名称：C3p0Util   
* 类描述：   
* 创建人：wizard   
* 创建时间：2012-3-31 上午10:26:50   
* 修改人：wizard   
* 修改时间：2012-3-31 上午10:26:50   
* 修改备注：   
* @version 0.9
*
 */
public class C3p0Util {

    private static ComboPooledDataSource dataSource = null;

    static {
        if (dataSource == null) {
            try {
                dataSource = new ComboPooledDataSource();
                dataSource.setDriverClass(Constants.rb.getString("db.driver"));
                dataSource.setJdbcUrl(Constants.rb.getString("db.url"));
                dataSource.setUser(Constants.rb.getString("db.user"));
                dataSource.setPassword(Constants.rb.getString("db.password"));
                dataSource.setMaxPoolSize(Integer.parseInt(Constants.rb
                        .getString("db.maxpoolsize")));
                dataSource.setMinPoolSize(Integer.parseInt(Constants.rb
                        .getString("db.minpoolsize")));
                dataSource.setInitialPoolSize(Integer.parseInt(Constants.rb
                        .getString("db.initpoolsize")));
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 从连接池中取一个连接
     * 
     * @return Connection
     * @throws SQLException
     */
    public static Connection getConn() throws SQLException {
        return dataSource.getConnection();
    }

    /**
     * 关闭连接
     * 
     * @param conn
     */
    public void closeConn(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
